<?php
/**
 * Copyright 2011 MaGdev
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 *
 *    * Redistributions of source code must retain the above copyright
 *      notice, this list of conditions and the following disclaimer.
 *    * Redistributions in binary form must reproduce the above copyright
 *      notice, this list of conditions and the following disclaimer in the
 *      documentation and/or other materials provided with the distribution.
 *    * The names of the authors may not be used to endorse or promote products
 *      derived from this software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
 * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
 * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 *
 * @package      Mashape API-Documentor
 * @author       Marco Graetsch <info@magdev.de>
 * @copyright    2011 Marco Graetsch.development
 * @license      http://opensource.org/licenses/bsd-license.php New BSD License
 * @link         https://code.google.com/p/apidocumentor/
 */



/**
 * Handler for Data-URI scheme
 *
 * @author magdev
 */
class Mashdoc_Helper_DataUri
{
	/** @var array */
	private $_options = array();
	
	/** @var string */
	private $_pattern = 'data:%s;base64,%s';
	
	
	/**
	 * Constructor
	 *
	 * @return void
	 */
	public function __construct($options = null)
	{
		$this->_options = $options;
	}
	
	
	/**
	 * Get the theme URL
	 *
	 * @return string
	 */
	public function getTheme()
	{
		$file = sprintf('assets/default/themes/%s.css', $this->_options['theme']);
		if (!$this->_options['useDataURI']) {
			return $file;
		}
		return $this->_toDataUri($file, 'text/css');
	}
	
	
	/**
	 * Get base CSS
	 *
	 * @return string
	 */
	public function getApiCss()
	{
		$file = $this->_options['cssFile'];
		if (!file_exists($this->_options['root'].'/'.$file)) {
		    return null;
		}
		if (!$this->_options['useDataURI']) {
			return $file;
		}
		return $this->_toDataUri($file, 'text/css');
	}
	
	
	/**
	 * Get the jquery URL
	 *
	 * @return string
	 */
	public function getJquery()
	{
		$file = sprintf('%s/jquery-%s.min.js', $this->_options['jqueryBaseURL'], $this->_options['jquery']);
		if (!$this->_options['useDataURI']) {
			return $file;
		}
		return $this->_toDataUri($file, 'text/javascript', false);
	}
	
	
	/**
	 * Get the jQuery mobile javascript URL
	 *
	 * @return string
	 */
	public function getJqueryMobileJs()
	{
		$file = sprintf('%s/mobile/%s/jquery.mobile-%s.min.js', $this->_options['jqueryBaseURL'], $this->_options['jquerymobile'], $this->_options['jquerymobile']);
		if (!$this->_options['useDataURI']) {
			return $file;
		}
		return $this->_toDataUri($file, 'text/javascript', false);
	}
	
	
	/**
	 * Get the jQuery mobile css URL
	 *
	 * @return string
	 */
	public function getJqueryMobileCss()
	{
		$file = sprintf('%s/mobile/%s/jquery.mobile-%s.min.css', $this->_options['jqueryBaseURL'], $this->_options['jquerymobile'], $this->_options['jquerymobile']);
		if (!$this->_options['useDataURI']) {
			return $file;
		}
		return $this->_toDataUri($file, 'text/css');
	}
	
	
	/**
	 * Get jQuery Mobile files as array
	 *
	 * @return array
	 */
	public function toArray()
	{
		return array(
			'themeCssURI' => $this->getTheme(),
			'apiCssURI' => $this->getApiCss(),
			'jqueryJsURI' => $this->getJquery(),
			'jqueryMobileJsURI' => $this->getJqueryMobileJs(),
			'jqueryMobileCssURI' => $this->getJqueryMobileCss(),
		);
	}
	
	
	/**
	 * Get a file as DataURI
	 *
	 * @param string $url
	 * @param boolean $parse
	 * @return string
	 */
	public function getDataUri($url, $parse = true)
	{
	    $ext = substr($url, strrpos($url, '.')+1);
	    $type = $this->_guessType($ext);
	    if (!$type) {
	        throw new InvalidArgumentException('Invalid file: '.$ext, Mashdoc::E_INVALID_ARGUMENT);
	    }
	    return $this->_toDataUri($url, $type, $parse);
	}
	
	
	/**
	 * Create a DataURI from a file or URL
	 *
	 * @param string $file
	 * @param string $type MIME-Type
	 * @param boolean $parse
	 * @return string
	 */
	private function _toDataUri($file, $type, $parse = true)
	{
		if ($parse) {
		    $cssParser = new Mashdoc_Helper_CssParser($file);
		    $content = $cssParser->parse();
		} else {
		    $content = file_get_contents($file);
		}
		return sprintf($this->_pattern, $type, base64_encode($content));
	}
	
	
	/**
	 * Get the MIME-Type by extension
	 *
	 * @param string $ext
	 * @return string
	 */
	private function _guessType($ext)
	{
	    $ext = strtolower($ext);
	    switch ($ext) {
	        case 'css':    return 'text/css';
	        case 'png':    return 'image/png';
	        case 'jpeg':
	        case 'jpg':    return 'image/jpg';
	        case 'gif':    return 'image/gif';
	        case 'js':     return 'application/javascript';
	        case 'json':   return 'application/x-json';
	    }
	}
}